#remotes::install_github("braverock/FactorAnalytics", build_vignettes = TRUE, force = TRUE)
pacman::p_load(tidyverse,tidyquant,FFdownload,FactorAnalytics,PerformanceAnalytics,PortfolioAnalytics,nloptr)
Please remember to put your assignment solutions in rmd format using many chunks and putting readable text in between, similar to my examples given in Research Methods and Assignment 1!
For all exercises: Please use the Assignment-Forum to post your questions, I will try my best to help you along!
In this exercise we want to estimate the CAPM. Please read carefully through the two documents provided (right hand side: files). Then we start to collect the necessary data:
LS&P100I (S&P 100): total return index (RI) and market cap (MV)library(readxl)
sp100_daily_RI <- read_excel("sp100 daily RI_2.xlsx")
head(sp100_daily_RI, n=10)
library(readxl)
sp100_monthly_MV <- read_excel("sp100 monthly MV.xlsx")
head(sp100_monthly_MV, n=10)
FFdownload). From both datasets we select data for the last (available) 60 months, calculate returns (simple percentage) for the US-Stocks and eliminate those stocks that have NAs for this period. tempf <- tempfile(fileext = ".RData"); tempd <- tempdir(); temptxt <- tempfile(fileext = ".txt")
inputlist <- c("F-F_Research_Data_Factors","F-F_Market_Beta")
FFdownload(exclude_daily=TRUE,tempdir=tempd,download=TRUE,download_only=FALSE,inputlist=inputlist)
Step 1: getting list of all the csv-zip-files!
Step 2: Downloading 2 zip-files
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/ME_Breakpoints_CSV.zip'
Step 3: Start processing 2 csv-files
|
| | 0%
|
|================================================ | 50%
|
|=================================================================================================| 100%
tempf2 <- tempfile(fileext = ".RData"); tempd2 <- tempdir()
FFdownload(output_file = tempf2,tempdir = tempd2,exclude_daily = TRUE, download = TRUE, download_only=FALSE, listsave=temptxt)
Step 1: getting list of all the csv-zip-files!
Step 2: Downloading 193 zip-files
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_Factors_weekly_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_5_Factors_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_ME_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_BE-ME_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_OP_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_INV_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_2x3_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_2x3_weekly_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_5x5_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/100_Portfolios_10x10_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/100_Portfolios_10x10_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_OP_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_OP_2x3_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_OP_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_OP_5x5_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/100_Portfolios_ME_OP_10x10_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/100_Portfolios_10x10_ME_OP_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_INV_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_INV_2x3_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_INV_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_INV_5x5_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/100_Portfolios_ME_INV_10x10_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/100_Portfolios_10x10_ME_INV_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_BEME_OP_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_BEME_OP_5x5_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_BEME_INV_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_BEME_INV_5x5_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_OP_INV_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_OP_INV_5x5_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/32_Portfolios_ME_BEME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/32_Portfolios_ME_BEME_OP_2x4x4_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/32_Portfolios_ME_BEME_INV_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/32_Portfolios_ME_BEME_INV_2x4x4_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/32_Portfolios_ME_OP_INV_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/32_Portfolios_ME_OP_INV_2x4x4_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_E-P_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_E-P_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_CF-P_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_CF-P_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_D-P_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_D-P_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_EP_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_EP_2x3_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_CFP_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_CFP_2x3_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_DP_2x3_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_DP_2x3_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Momentum_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/10_Portfolios_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_ST_Reversal_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_Prior_1_0_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_Prior_1_0_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/10_Portfolios_Prior_1_0_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_LT_Reversal_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/6_Portfolios_ME_Prior_60_13_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_Prior_60_13_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/10_Portfolios_Prior_60_13_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_AC_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_AC_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_BETA_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_BETA_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_NI_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_NI_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_VAR_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_VAR_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_RESVAR_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/25_Portfolios_ME_RESVAR_5x5_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/5_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/5_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/10_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/10_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/12_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/12_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/17_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/17_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/30_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/30_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/38_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/38_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/48_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/48_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/49_Industry_Portfolios_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/49_Industry_Portfolios_Wout_Div_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/ME_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/BE-ME_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/OP_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/INV_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/E-P_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/CF-P_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/D-P_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Prior_2-12_Breakpoints_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_3_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_3_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_3_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_3_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_3_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_3_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_Mom_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_Mom_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_Mom_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_Mom_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_MOM_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_Mom_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_25_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_25_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_25_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_25_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_25_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_25_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_25_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_25_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_25_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_25_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_25_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_25_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_25_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_25_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_25_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_25_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_25_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_25_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_25_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_32_Portfolios_ME_BE-ME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_32_Portfolios_ME_BE-ME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_32_Portfolios_ME_BE-ME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_32_Portfolios_ME_BE-ME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_32_Portfolios_ME_BE-ME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_32_Portfolios_ME_BE-ME_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_32_Portfolios_ME_BE-ME_INV(TA)_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_32_Portfolios_ME_BE-ME_INV(TA)_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_32_Portfolios_ME_BE-ME_INV(TA)_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_32_Portfolios_ME_BE-ME_INV(TA)_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_32_Portfolios_ME_BE-ME_INV(TA)_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_32_Portfolios_ME_BE-ME_INV(TA)_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_32_Portfolios_ME_INV(TA)_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Developed_ex_US_32_Portfolios_ME_INV(TA)_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Europe_32_Portfolios_ME_INV(TA)_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Japan_32_Portfolios_ME_INV(TA)_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Asia_Pacific_ex_Japan_32_Portfolios_ME_INV(TA)_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/North_America_32_Portfolios_ME_INV(TA)_OP_2x4x4_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_5_Factors_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_MOM_Factor_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_6_Portfolios_ME_BE-ME_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_6_Portfolios_ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_6_Portfolios_ME_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_6_Portfolios_ME_Prior_12_2_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_4_Portfolios_BE-ME_OP_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_4_Portfolios_OP_INV_CSV.zip'
trying URL 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Emerging_Markets_4_Portfolios_BE-ME_INV_CSV.zip'
Step 3: Start processing 193 csv-files
|
| | 0%
|
|= | 1%
|
|== | 2%
|
|=== | 3%
NAs introduced by coercioninput string 15 is invalid in this locale
|
|==== | 4%
NAs introduced by coercionNAs introduced by coercion
|
|===== | 5%
NAs introduced by coercioninput string 15 is invalid in this locale
|
|====== | 6%
|
|======= | 7%
|
|======== | 8%
|
|========= | 9%
|
|========== | 10%
input string 16 is invalid in this localeinput string 16 is invalid in this locale
|
|=========== | 11%
|
|============ | 12%
|
|============= | 13%
input string 15 is invalid in this locale
|
|============== | 14%
input string 15 is invalid in this locale
|
|============== | 15%
|
|=============== | 15%
|
|=============== | 16%
|
|================ | 16%
|
|================ | 17%
|
|================= | 17%
input string 17 is invalid in this locale
|
|================= | 18%
input string 17 is invalid in this locale
|
|================== | 18%
|
|================== | 19%
|
|=================== | 19%
|
|=================== | 20%
|
|==================== | 20%
input string 16 is invalid in this locale
|
|==================== | 21%
input string 16 is invalid in this locale
|
|===================== | 21%
input string 17 is invalid in this locale
|
|===================== | 22%
input string 17 is invalid in this locale
|
|====================== | 22%
|
|====================== | 23%
|
|======================= | 23%
|
|======================= | 24%
|
|======================== | 24%
|
|======================== | 25%
|
|========================= | 25%
|
|========================= | 26%
|
|========================== | 26%
|
|========================== | 27%
|
|=========================== | 27%
|
|=========================== | 28%
|
|============================ | 28%
|
|============================ | 29%
|
|============================= | 30%
|
|============================== | 31%
|
|=============================== | 32%
input string 15 is invalid in this locale
|
|================================ | 33%
input string 15 is invalid in this locale
|
|================================= | 34%
|
|================================== | 35%
|
|=================================== | 36%
|
|==================================== | 37%
|
|===================================== | 38%
|
|====================================== | 39%
|
|======================================= | 40%
|
|======================================== | 41%
|
|========================================= | 42%
|
|========================================== | 43%
|
|========================================== | 44%
|
|=========================================== | 44%
|
|=========================================== | 45%
|
|============================================ | 45%
|
|============================================ | 46%
|
|============================================= | 46%
|
|============================================= | 47%
|
|============================================== | 47%
|
|============================================== | 48%
|
|=============================================== | 48%
|
|=============================================== | 49%
|
|================================================ | 49%
|
|================================================ | 50%
|
|================================================= | 50%
|
|================================================= | 51%
|
|================================================== | 51%
|
|================================================== | 52%
|
|=================================================== | 52%
|
|=================================================== | 53%
|
|==================================================== | 53%
|
|==================================================== | 54%
|
|===================================================== | 54%
|
|===================================================== | 55%
|
|====================================================== | 55%
|
|====================================================== | 56%
|
|======================================================= | 56%
|
|======================================================= | 57%
|
|======================================================== | 58%
|
|========================================================= | 59%
|
|========================================================== | 60%
|
|=========================================================== | 61%
|
|============================================================ | 62%
|
|============================================================= | 63%
|
|============================================================== | 64%
|
|=============================================================== | 65%
|
|================================================================ | 66%
|
|================================================================= | 67%
|
|================================================================== | 68%
|
|=================================================================== | 69%
|
|==================================================================== | 70%
|
|===================================================================== | 71%
|
|===================================================================== | 72%
|
|====================================================================== | 72%
|
|====================================================================== | 73%
|
|======================================================================= | 73%
|
|======================================================================= | 74%
|
|======================================================================== | 74%
|
|======================================================================== | 75%
|
|========================================================================= | 75%
|
|========================================================================= | 76%
|
|========================================================================== | 76%
|
|========================================================================== | 77%
|
|=========================================================================== | 77%
|
|=========================================================================== | 78%
|
|============================================================================ | 78%
|
|============================================================================ | 79%
|
|============================================================================= | 79%
|
|============================================================================= | 80%
|
|============================================================================== | 80%
|
|============================================================================== | 81%
|
|=============================================================================== | 81%
|
|=============================================================================== | 82%
|
|================================================================================ | 82%
|
|================================================================================ | 83%
|
|================================================================================= | 83%
|
|================================================================================= | 84%
|
|================================================================================== | 84%
|
|================================================================================== | 85%
|
|=================================================================================== | 85%
|
|=================================================================================== | 86%
|
|==================================================================================== | 87%
|
|===================================================================================== | 88%
|
|====================================================================================== | 89%
|
|======================================================================================= | 90%
|
|======================================================================================== | 91%
|
|========================================================================================= | 92%
|
|========================================================================================== | 93%
|
|=========================================================================================== | 94%
|
|============================================================================================ | 95%
|
|============================================================================================= | 96%
|
|============================================================================================== | 97%
input string 17 is invalid in this locale
|
|=============================================================================================== | 98%
input string 17 is invalid in this locale
|
|================================================================================================ | 99%
|
|=================================================================================================| 100%
load(tempf2)
ff<-FFdownload$x_Developed_ex_US_3_Factors$monthly$Temp2
sff<-ff["2015/2019"]
sff<-timetk::tk_tbl(ff)
colnames(sff)[1]<-"date"
sff
require(tidyquant)
require(timetk)
Loading required package: timetk
anyNA(sp100_daily_RI)
[1] FALSE
sp100_daily_RI_prices <- gather(sp100_daily_RI, key = symbol, value= prices, "AMAZON.COM":"CHARTER COMMS.CL.A")
anyNA(sp100_daily_RI_prices)
[1] FALSE
sp100_returns_RI_60_long <- sp100_daily_RI_prices %>% mutate(prices = as.numeric(prices)) %>% group_by(symbol) %>%
tq_transmute(select = prices,
mutate_fun = periodReturn,
period="monthly",
type="arithmetic",
col_rename = "Stock.returns") %>% ungroup() %>% mutate(date = as.yearmon(date))
anyNA(sp100_returns_RI_60_long)
[1] FALSE
sp100_returns_RI_60_long <- sp100_returns_RI_60_long[c(2,1,3)] %>% group_by(symbol)
fama_french <- sff %>%
select(date, Mkt.RF, RF) %>% mutate(date = as.yearmon(date))
mutate and map in combination with lm). Estimate the mean-return for each stock and plot the return/beta-combinations. Create the security market line and include it in the plot! What do you find? library(tidyquant)
library(tidyverse)
library(PerformanceAnalytics)
joined_data <- left_join(sp100_returns_RI_60_long, fama_french, by= c("date"))
joined_data <- mutate(joined_data,
monthly_ret_rf = Stock.returns - RF)
require(xts)
regr_fun <- function(data_xts) {
lm(monthly_ret_rf ~ Mkt.RF, data = as_data_frame(data_xts)) %>%
coef()
}
beta_alpha <- joined_data %>%
tq_mutate(mutate_fun = rollapply,
width = 60,
FUN = regr_fun,
by.column = FALSE,
col_rename = c("alpha", "beta"))
Problem with `mutate()` input `nested.col`.
ℹ `as_data_frame()` is deprecated as of tibble 2.0.0.
Please use `as_tibble()` instead.
The signature and semantics have changed, see `?as_tibble`.
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated.
ℹ Input `nested.col` is `purrr::map(...)`.
ℹ The error occurred in group 1: symbol = "3M".`as_data_frame()` is deprecated as of tibble 2.0.0.
Please use `as_tibble()` instead.
The signature and semantics have changed, see `?as_tibble`.
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated.
beta_alpha
beta_alpha_filter <- filter(beta_alpha, !is.na(alpha))
symbol_beta_alpha <- beta_alpha_filter %>%
select(symbol, alpha, beta)
symbol_beta_alpha
alpha <- beta_alpha %>%
select(symbol, alpha)
beta <- beta_alpha_filter %>%
select(symbol, beta)
beta
Correction:
library(dplyr)
means_sp100_RI_60 <- joined_data %>%
dplyr::group_by(symbol) %>%
dplyr::summarize(mu = mean(monthly_ret_rf, na.rm=TRUE))
`summarise()` ungrouping output (override with `.groups` argument)
means_sp100_RI_60
mu.hat <- mutate(beta_alpha,
mu_capm = beta * mean(Mkt.RF))
mu.hat <- filter(mu.hat, !is.na(alpha))
mu.hat <- mu.hat %>%
select(symbol, alpha, beta, mu_capm)
mu.hat <- merge(mu.hat, means_sp100_RI_60)
sml.fit <- lm(mu_capm~beta, mu.hat)
install.packages("plotly")
Error in install.packages : Updating loaded packages
library(plotly)
Attaching package: ‘plotly’
The following objects are masked from ‘package:plyr’:
arrange, mutate, rename, summarise
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
p <- plot_ly(mu.hat, x = ~beta, y = ~mu_capm, type = 'scatter', mode = 'line', text = ~paste('symbol:', symbol)) %>%
add_markers(x = ~beta, y = ~mu)
p
`arrange_()` is deprecated as of dplyr 0.7.0.
Please use `arrange()` instead.
See vignette('programming') for more help
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated.
sp100_monthly_MV <- read_excel("sp100 monthly MV.xlsx")
head(sp100_monthly_MV, n=10)
anyNA(sp100_monthly_MV)
[1] FALSE
sp100_monthly_MV <- gather(sp100_monthly_MV, key = symbol, value= value, "AMAZON.COM":"CHARTER COMMS.CL.A")
anyNA(sp100_daily_RI_prices)
[1] FALSE
mean value
mean_sp100_MV <- sp100_monthly_MV %>%
group_by(symbol) %>%
summarize(mean_value = mean(value, na.rm=TRUE))
symbol_beta_alpha_value <- merge(mean_sp100_MV, beta)
symbol_beta_alpha_value <- arrange(symbol_beta_alpha_value, beta)
symbol_beta_alpha_value
create weights
Portfolio1 <- symbol_beta_alpha_value[1:10,]
sum_weights1 <- sum(Portfolio1$mean_value)
weight_portfolio1 <- Portfolio1$mean_value/sum_weights1
Portfolio2 <- symbol_beta_alpha_value[11:20,]
sum_weights2 <- sum(Portfolio2$mean_value)
weight_portfolio2 <- Portfolio2$mean_value/sum_weights2
Portfolio3 <- symbol_beta_alpha_value[21:30,]
sum_weights3 <- sum(Portfolio3$mean_value)
weight_portfolio3 <- Portfolio3$mean_value/sum_weights3
Portfolio4 <- symbol_beta_alpha_value[31:40,]
sum_weights4 <- sum(Portfolio4$mean_value)
weight_portfolio4 <- Portfolio4$mean_value/sum_weights4
Portfolio5 <- symbol_beta_alpha_value[41:50,]
sum_weights5 <- sum(Portfolio5$mean_value)
weight_portfolio5 <- Portfolio5$mean_value/sum_weights5
Portfolio6 <- symbol_beta_alpha_value[51:60,]
sum_weights6 <- sum(Portfolio6$mean_value)
weight_portfolio6 <- Portfolio6$mean_value/sum_weights6
Portfolio7 <- symbol_beta_alpha_value[61:70,]
sum_weights7 <- sum(Portfolio7$mean_value)
weight_portfolio7 <- Portfolio7$mean_value/sum_weights7
Portfolio8 <- symbol_beta_alpha_value[71:80,]
sum_weights8 <- sum(Portfolio8$mean_value)
weight_portfolio8 <- Portfolio8$mean_value/sum_weights8
Portfolio9 <- symbol_beta_alpha_value[81:90,]
sum_weights9 <- sum(Portfolio9$mean_value)
weight_portfolio9 <- Portfolio9$mean_value/sum_weights9
Portfolio10 <- symbol_beta_alpha_value[91:nrow(symbol_beta_alpha_value),]
sum_weights10 <- sum(Portfolio10$mean_value)
weight_portfolio10 <- Portfolio10$mean_value/sum_weights10
returns Portfolio
sp100_returns_RI_60_wide <- sp100_returns_RI_60_long %>% spread(symbol, Stock.returns)
portfolio1_returns_wide <- sp100_returns_RI_60_wide[c("date", Portfolio1$symbol)]
portfolio1_returns_long <- gather(portfolio1_returns_wide, key = symbol, value= returns, c(Portfolio1$symbol))
portfolio1_returns<- portfolio1_returns_long %>%
tq_portfolio(assets_col = symbol,
returns_col = returns,
weights = weight_portfolio1,
col_rename = "Portfolio.returns") %>% mutate(Portfolio = "Portfolio 1")
portfolio2_returns_wide <- sp100_returns_RI_60_wide[c("date", Portfolio2$symbol)]
portfolio2_returns_long <- gather(portfolio2_returns_wide, key = symbol, value= returns, c(Portfolio2$symbol))
portfolio2_returns<- portfolio2_returns_long %>%
tq_portfolio(assets_col = symbol,
returns_col = returns,
weights = weight_portfolio2,
col_rename = "Portfolio.returns") %>% mutate(Portfolio = "Portfolio 2")
portfolio3_returns_wide <- sp100_returns_RI_60_wide[c("date", Portfolio3$symbol)]
portfolio3_returns_long <- gather(portfolio3_returns_wide, key = symbol, value= returns, c(Portfolio3$symbol))
portfolio3_returns<- portfolio3_returns_long %>%
tq_portfolio(assets_col = symbol,
returns_col = returns,
weights = weight_portfolio3,
col_rename = "Portfolio.returns") %>% mutate(Portfolio = "Portfolio 3")
portfolio4_returns_wide <- sp100_returns_RI_60_wide[c("date", Portfolio4$symbol)]
portfolio4_returns_long <- gather(portfolio4_returns_wide, key = symbol, value= returns, c(Portfolio4$symbol))
portfolio4_returns<- portfolio4_returns_long %>%
tq_portfolio(assets_col = symbol,
returns_col = returns,
weights = weight_portfolio4,
col_rename = "Portfolio.returns") %>% mutate(Portfolio = "Portfolio 4")
portfolio5_returns_wide <- sp100_returns_RI_60_wide[c("date", Portfolio5$symbol)]
portfolio5_returns_long <- gather(portfolio5_returns_wide, key = symbol, value= returns, c(Portfolio5$symbol))
portfolio5_returns<- portfolio5_returns_long %>%
tq_portfolio(assets_col = symbol,
returns_col = returns,
weights = weight_portfolio5,
col_rename = "Portfolio.returns") %>% mutate(Portfolio = "Portfolio 5")
portfolio6_returns_wide <- sp100_returns_RI_60_wide[c("date", Portfolio6$symbol)]
portfolio6_returns_long <- gather(portfolio6_returns_wide, key = symbol, value= returns, c(Portfolio6$symbol))
portfolio6_returns<- portfolio6_returns_long %>%
tq_portfolio(assets_col = symbol,
returns_col = returns,
weights = weight_portfolio6,
col_rename = "Portfolio.returns") %>% mutate(Portfolio = "Portfolio 6")
portfolio7_returns_wide <- sp100_returns_RI_60_wide[c("date", Portfolio7$symbol)]
portfolio7_returns_long <- gather(portfolio7_returns_wide, key = symbol, value= returns, c(Portfolio7$symbol))
portfolio7_returns<- portfolio7_returns_long %>%
tq_portfolio(assets_col = symbol,
returns_col = returns,
weights = weight_portfolio7,
col_rename = "Portfolio.returns") %>% mutate(Portfolio = "Portfolio 7")
portfolio8_returns_wide <- sp100_returns_RI_60_wide[c("date", Portfolio8$symbol)]
portfolio8_returns_long <- gather(portfolio8_returns_wide, key = symbol, value= returns, c(Portfolio8$symbol))
portfolio8_returns<- portfolio8_returns_long %>%
tq_portfolio(assets_col = symbol,
returns_col = returns,
weights = weight_portfolio8,
col_rename = "Portfolio.returns") %>% mutate(Portfolio = "Portfolio 8")
portfolio9_returns_wide <- sp100_returns_RI_60_wide[c("date", Portfolio9$symbol)]
portfolio9_returns_long <- gather(portfolio9_returns_wide, key = symbol, value= returns, c(Portfolio9$symbol))
portfolio9_returns<- portfolio9_returns_long %>%
tq_portfolio(assets_col = symbol,
returns_col = returns,
weights = weight_portfolio9,
col_rename = "Portfolio.returns") %>% mutate(Portfolio = "Portfolio 9")
portfolio10_returns_wide <- sp100_returns_RI_60_wide[c("date", Portfolio10$symbol)]
portfolio10_returns_long <- gather(portfolio10_returns_wide, key = symbol, value= returns, c(Portfolio10$symbol))
portfolio10_returns<- portfolio10_returns_long %>%
tq_portfolio(assets_col = symbol,
returns_col = returns,
weights = weight_portfolio10,
col_rename = "Portfolio.returns") %>% mutate(Portfolio = "Portfolio 10")
allportfolio_returns <- rbind(portfolio1_returns, portfolio2_returns, portfolio3_returns, portfolio4_returns, portfolio5_returns, portfolio6_returns, portfolio7_returns, portfolio8_returns, portfolio9_returns, portfolio10_returns)
allportfolio_returns <- allportfolio_returns %>% group_by(Portfolio)
allportfolio_returns
joined_data_portfolio <- left_join(allportfolio_returns, fama_french, by= c("date"))
joined_data_portfolio <- mutate(joined_data_portfolio,
monthly_ret_rf = Portfolio.returns - RF)
require(xts)
regr_fun_portfolio <- function(data_xts) {
lm(monthly_ret_rf ~ Mkt.RF, data = as_data_frame(data_xts)) %>%
coef()
}
beta_alpha_portfolio <- joined_data_portfolio %>%
tq_mutate(mutate_fun = rollapply,
width = 60,
FUN = regr_fun_portfolio,
by.column = FALSE,
col_rename = c("alpha", "beta"))
beta_alpha_portfolio
beta_alpha_portfolio_filter <- filter(beta_alpha_portfolio, !is.na(alpha))
symbol_beta_alpha_portfolio <- beta_alpha_portfolio_filter %>%
select(Portfolio, alpha, beta)
symbol_beta_alpha_portfolio
alpha_portfolio <- beta_alpha_portfolio %>%
select(Portfolio, alpha)
beta_portfolio <- beta_alpha_portfolio %>%
select(Portfolio, beta)
Correction:
means_Portfolio <- joined_data_portfolio %>%
dplyr::group_by(Portfolio) %>%
dplyr::summarize(mu = mean(monthly_ret_rf, na.rm=TRUE))
`summarise()` ungrouping output (override with `.groups` argument)
means_Portfolio
return_beta_portfolio <- merge(means_Portfolio, beta_portfolio)
mu.hat_portfolio <- mutate(beta_alpha_portfolio,
mu_capm_portfolio = beta * mean(Mkt.RF))
mu.hat_portfolio <- filter(mu.hat_portfolio, !is.na(alpha))
mu.hat_portfolio <- mu.hat_portfolio %>%
select(Portfolio, alpha, beta, mu_capm_portfolio)
mu.hat_portfolio <- merge(mu.hat_portfolio, means_Portfolio)
sml.fit_portfolio <- lm(mu_capm_portfolio~beta, mu.hat_portfolio)
library(plotly)
p_portfolio <- plot_ly(mu.hat_portfolio, x = ~beta, y = ~mu_capm_portfolio, type = 'scatter', mode = 'line', text = ~paste('Portfolio:', Portfolio)) %>%
add_markers(x = ~beta, y = ~mu)
p_portfolio
require(xts)
regr_fun_residuals <- function(data_xts) {
data_xts <- lm(monthly_ret_rf ~ Mkt.RF, data = as_data_frame(data_xts))
R <- summary(data_xts)$sigma^2
return(R)
}
residuals <- joined_data %>%
tq_mutate(mutate_fun = rollapply,
width = 60,
FUN = regr_fun_residuals,
by.column = FALSE,
col_rename = c("Residuals"))
residuals_only <- filter(residuals, !is.na(Residuals))
symbol_residuals <- residuals_only %>%
select(symbol, Residuals)
mean_MKt.RF <- joined_data %>%
group_by(symbol) %>%
summarize(mu_MKt_RF = mean(Mkt.RF, na.rm=TRUE))
first <- left_join(symbol_beta_alpha, symbol_residuals, by = "symbol" )
second <- merge(mean_MKt.RF, means_sp100_RI_60)
all_inputs <- merge(first, second)
second_pass_regression <- lm(mu~ beta + Residuals, all_inputs)
summary(second_pass_regression)
Call:
lm(formula = mu ~ beta + Residuals, data = all_inputs)
Residuals:
Min 1Q Median 3Q Max
-0.0259042 -0.0053882 -0.0002212 0.0053325 0.0307786
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.065691 0.003284 -20.004 <2e-16 ***
beta 0.200369 0.266416 0.752 0.454
Residuals 0.173445 0.388579 0.446 0.656
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.008921 on 94 degrees of freedom
Multiple R-squared: 0.01422, Adjusted R-squared: -0.006757
F-statistic: 0.6778 on 2 and 94 DF, p-value: 0.5102
p
require(xts)
regr_fun_residuals <- function(data_xts) {
data_xts <- lm(monthly_ret_rf ~ Mkt.RF, data = as_data_frame(data_xts))
R <- summary(data_xts)$sigma^2
return(R)
}
residuals_portfolio <- joined_data_portfolio %>%
tq_mutate(mutate_fun = rollapply,
width = 60,
FUN = regr_fun_residuals,
by.column = FALSE,
col_rename = c("Residuals"))
residuals_only_portfolio <- filter(residuals_portfolio, !is.na(Residuals))
symbol_residuals_portfolio <- residuals_only_portfolio %>%
select(Portfolio, Residuals)
mean_MKt.RF_portfolio <- joined_data_portfolio %>%
group_by(Portfolio) %>%
summarize(mu_MKt_RF = mean(Mkt.RF, na.rm=TRUE))
first_portfolio <- left_join(symbol_beta_alpha_portfolio, symbol_residuals_portfolio, by = "Portfolio" )
second_portfolio <- merge(mean_MKt.RF_portfolio, means_Portfolio)
all_inputs_portfolio <- merge(first_portfolio, second_portfolio)
second_pass_regression_portfolio <- lm(mu~ beta + Residuals, all_inputs_portfolio)
summary(second_pass_regression_portfolio)
Call:
lm(formula = mu ~ beta + Residuals, data = all_inputs_portfolio)
Residuals:
Min 1Q Median 3Q Max
-0.0045091 -0.0012808 0.0001732 0.0014667 0.0056631
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.078676 0.006378 -12.335 5.28e-06 ***
beta 0.133825 0.354192 0.378 0.7168
Residuals 2.506199 1.234101 2.031 0.0818 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.003436 on 7 degrees of freedom
Multiple R-squared: 0.5783, Adjusted R-squared: 0.4579
F-statistic: 4.8 on 2 and 7 DF, p-value: 0.04869
p_portfolio
Read Chapter 24 of our book. In this exercise use a Minimum Variance and a Tangecy (maxium Sharpe Ratio) portfolio calculate from your stocks, as well as the S&P500 as a benchmark (Period 2000-01-01 - 2020-01-11). For all three Investment Opportunities imagine you invest 100USD per month into the portfolio. What is the overall return this investment provides you? How much should you have investd at the bginning (one-time investment) to get the eact same overall wealth at the end of 2020? Can you plot both wealth developments over time?
First of all, I am going to download all the crucial data, which we need in order to create the three portfolios. My personal stock choice are Apple, Nividia, Microsoft, American Express, Walmart, Bank of America, Morgan Stanley, Disney, Exxon Mobile. As the benchmark we take the S&P500, regarding to the underling task.
install.packages("FinCal",dependencies=TRUE)
library(timetk, PortfolioAnalytics)
SP500 <- tq_index("SP500")
stock_names <- c("AAPL", "NVDA", "MSFT", "AXP", "WMT", "BAC", "GS","MS", "DIS", "XOM")
stock_returns <-tq_get(x = stock_names,get = "stock.prices", from = "2000-01-01", to = "2020-11-18") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly")
unique(stock_returns$symbol)
stock_returns_xts <- stock_returns %>%
subset( select = c(symbol,date, monthly.returns)) %>%
pivot_wider(names_from = symbol,
values_from = monthly.returns) %>%
tk_xts(date_var = date, silent = TRUE)
stock_returns_xts
port <- portfolio.spec(assets=stock_names)%>%
add.constraint(type="full_investment") %>% #In our approach we use full_investment as our task it is
add.constraint(type="long_only") %>% #We do not take any short-selling
add.objective(type="return", name="mean")
port
Now I defined the investment constrains for our investments, which is based on our task a full investment constrained, due to the fact that we would like to be fully invested the whole period. The long only constrain comes from the idea that we are just caring about the upper part of the efficient frontier, in order to construct the tangency portfolio, which is the most efficient way to set our 10 stocks together concerning the faced risked. So, in order words the tangency portfolio maximizes the Sharpe Ratio. The second portfolio we have to construct is the Minimum-Variance Portfolio. The “third” portfolio would be an absolute passive strategy we would just invest in the SP500.
opt_port <- optimize.portfolio(R=stock_returns_xts, portfolio=port,
optimize_method="ROI", trace=TRUE)
opt_port
So, as we see we would make the most money if we would just everything in Nividia. However, we do not look for the maximum return, we look for the maximum return in relation to the taken risk.
plot(opt_port, chart.assets=TRUE, main="Maximum Return",
xlim=c(0,0.3), ylim=c(0,0.5))
chart.RiskReward(opt_port,return.col="mean", risk.col="sd",
chart.assets=TRUE,
xlim=c(0, 0.25),
main="Maximum Return")
frontier <- create.EfficientFrontier(R=stock_returns_xts,
portfolio=port,
type="mean-StdDev")
chart.EfficientFrontier(frontier, match.col="StdDev", type="l",rf = 0, tangent.line = TRUE, chart.assets = TRUE,)
The efficient frontier is the set of optimal portfolios that offer the highest expected return for a defined level of risk or the lowest risk for a given level of expected return. Portfolios that lie below the efficient frontier are sub-optimal because they do not provide enough return for the level of risk.
port_tan <- portfolio.spec(assets=stock_names) %>%
add.constraint(type="full_investment") %>%
add.constraint(type = "long_only") %>%
add.objective(type="return", name="mean")
port_tan
init.portf <- portfolio.spec(assets=stock_names)
init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
init.portf <- add.constraint(portfolio=init.portf, type="long_only")
init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
init.portf
Maximizing the Sharpe Ratio can be formulated as a quadratic programming problem and solved very quickly using optimize_method=“ROI”. Although “StdDev” was specified as an objective, the quadratic programming problem uses the variance-covariance matrix in the objective function.
The default actin if “mean” and “StdDev” are specified as objectives with optimize_method=“ROI” is to maximize quadratic utility. If we want to maximize Sharpe Ratio, we need to pass in maxSR=TRUE to optimize the portfolio, which should lead us to the tangency portfolio.
maxSR.lo.ROI <- optimize.portfolio(R=stock_returns_xts, portfolio=init.portf, optimize_methode="ROI", maxSR=TRUE, trace=TRUE, search_size= 1000000)
maxSR.lo.ROI
It needed 17 Iterations to come as close as possible to the tangeny portfolio. We would put a big stake into Walmart, Exxon-Mobil, Disney. Although the maximum Sharpe Ratio objective can be solved quickly and accurately with optimize_method=“ROI”, it is also possible or DEoptim. These solvers have the added flexibility of using different methods to calculate the Sharpe Ratio (e.g. we could specify annualized measures of risk and return).
maxSR.lo.RP <- optimize.portfolio(R=stock_returns_xts, portfolio=init.portf, optimize_method="random", search_size=1000000, trace=TRUE)
maxSR.lo.RP
chart.RiskReward(maxSR.lo.RP, risk.col="StdDev", return.col="mean")
maxSR.lo.DE <- optimize.portfolio(R=stock_returns_xts, portfolio=init.portf, optimize_method="DEoptim", search_size=1000000, trace=TRUE)
maxSR.lo.DE
chart.RiskReward(maxSR.lo.DE, risk.col="StdDev", return.col="mean")
The Minmimum Variance Portfolio minimizing the variance of the portfolio
port_minvar <- portfolio.spec(assets=stock_names) %>%
add.constraint(type="full_investment") %>%
add.constraint(type = "long_only") %>%
add.objective(type="risk", name="var")
port_minvar
opt_minvar <- optimize.portfolio(R = stock_returns_xts,portfolio = port_minvar,
optimize_method = "ROI", trace = TRUE, search_size= 1000000)
opt_minvar
So, as we can see we see that we would invest the majority of our capital in Walmart and Exxon Mobile, due to the low standard deviation and quiet interesting we would not invest in Nividia.
chart.RiskReward(opt_minvar,return.col="mean", risk.col="sd",
chart.assets=TRUE,
xlim=c(0.01, 0.25),
main="Minimum Variance")
wts_tan <- c(0.104, 0.002, 0.120, 0.002, 0.444, 0.006, 0.026, 0.000, 0.094, 0.202)
wts_minvar <- c(0.0241, 0.0000, 0.0615, 0.0000, 0.4551, 0.0000, 0.0000, 0.0000, 0.1094, 0.3499)# Weights from the Minimum Variance Portfolio
Portfolio Returns
stock_returns
tan_port <- stock_returns %>%
tq_portfolio(assets_col = symbol,
returns_col = monthly.returns,
weights = wts_tan,
col_rename = "Return")
tan_port
minvar_port <- stock_returns %>%
tq_portfolio(assets_col = symbol,
returns_col = monthly.returns,
weights = wts_minvar,
col_rename = "Return")
minvar_port
For this task we take the SP500 as the benchmark.
bench_returns <- "^GSPC" %>%
tq_get(get = "stock.prices",
from = "2000-01-01",
to = "2020-11-17") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Benchmark")
bench_returns
ov_tan_port <- left_join(tan_port,bench_returns,by = "date")
ov_minvar_port <- left_join(minvar_port,bench_returns,by = "date")
ov_tan_port
ov_minvar_port
ov_tan_port %>%
tq_performance(Ra = Return, Rb = Benchmark,
performance_fun = table.CAPM)
ov_minvar_port %>%
tq_performance(Ra = Return, Rb = Benchmark,
performance_fun = table.CAPM)
sharp_tan <- (0.1112-0)/sd(ov_tan_port$Return)
sharp_minvar <- (0.0605-0)/sd(ov_minvar_port$Return)
sharp_bench <- 1 #Checked it on the internet on an average base it is close to 1
ov_sharp <- cbind(sharp_tan, sharp_minvar, sharp_bench)
ov_sharp
tan_r <- geometric.mean(ov_tan_port$Return)
minvar_r <- geometric.mean(ov_minvar_port$Return)
bench_r <- geometric.mean(ov_minvar_port$Benchmark)
FV_tan <- FV(rate = tan_r, nper = 1:239,pv = 100,pmt = -100,type = 1)
FV_minvar <- FV(rate = minvar_r, nper = 1:239, pv = 100, pmt = -100, type=1)
FV_bench <- FV(rate=bench_r, nper = 1:239, pv = 100, pmt = -100, type = 1)
id <- c(1:239)
as.data.frame(FV_tan)
as.data.frame(id)
as.data.frame(FV_minvar)
as.data.frame(FV_bench)
ov_list <- cbind(id, FV_tan,FV_minvar,FV_bench)
ov_list <- as.data.frame(ov_list)
invest <- ov_list[239,]
invest
ggplot(ov_list, aes(x=id)) +
geom_line(aes(y = FV_tan), color = "darkred") +
geom_line(aes(y = FV_minvar), color="steelblue", linetype="twodash") +
geom_line(aes(y=FV_bench), color="pink") +
ggtitle("Wealth Development")
The tangency portfolio performed very good related to the others.
PV_tan <- PV(rate = tan_r,nper = 1:239,fv =-invest$FV_tan ,pmt = 0,type = 1)
PV_minvar <- PV(rate =minvar_r, nper=1:239,fv=-invest$FV_minvar,pmt=0,type=1)
PV_bench <- PV(rate=bench_r, nper=1:239, fv=-invest$FV_bench,pmt=0, type=1)
id <- c(1:239)
as.data.frame(PV_tan)
as.data.frame(id)
as.data.frame(PV_minvar)
as.data.frame(PV_bench)
ov_list <- cbind(id, PV_tan,PV_minvar,PV_bench)
ov_list <- as.data.frame(ov_list)
invest <- ov_list[239,]
invest
As we see if we would like to make an one time investment, but also the exact same amount of money as if we would invest 100 dollar per month. For the Tangency Portfolio we would need to invest 3109.721$ for the Minimium Variance Portfolio we would need to invest 4695.70$ and for the passive strategy S&P500 we would need 5082.05. So, as we can see that the intrest of intrest effect takes here part and dramatically increases the future value of our investment.
ggplot(ov_list, aes(x=id)) +
geom_line(aes(y = PV_tan), color = "darkred") +
geom_line(aes(y = PV_minvar), color="steelblue", linetype="twodash") +
geom_line(aes(y=PV_bench), color="pink") +
ggtitle("One time investment")
For the same two portfolios and the appropriate benchmark calculate overall performance measures (Sharpe ratio, M2 [assume a risk-fre rate of 0], Treynor Ratio, Jensen’s Alpha and Information ratio). Interpret. Additional do the two market timing regressions (ch 24.4) and see whether your portfolios can “time” the market.
Work through trough the demo demo(relative_ranking). Use what you lear here, form an appropriate opinion on the ranking of your assets and optimize a Minimum Variance and Maximum Sharpe ratio Portfolio. Which one performs better?
demo(relative_ranking)
data(edhec)
library(PortfolioAnalytics)
data(edhec)
R <- edhec[,1:4]
funds <- colnames(R)
#’ Construct initial portfolio with basic constraints.
init.portf <- portfolio.spec(assets=funds)
init.portf <- add.constraint(portfolio=init.portf, type="weight_sum",
min_sum=0.99, max_sum=1.01)
init.portf <- add.constraint(portfolio=init.portf, type="box",min=0.05, max=0.5)
init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
init.portf <- add.objective(portfolio=init.portf, type="return", name="mean")
init.portf
#’ Here we express views on the relative rank of the asset returns.
asset.rank <- c(2, 3, 1, 4)
#’ Use Meucci Fully Flexible Views framework to express views on the relative #’ order of asset returns. #’ Define prior probabilities.
p <- rep(1 / nrow(R), nrow(R))
#’ Express view on the relative ordering of asset returns
m.moments <- meucci.ranking(R, p, asset.rank)
#’ Express views using the method described in Almgren and Chriss, > #’ “Portfolios from Sorts”.
ac.moments <- list()
ac.moments
ac.moments$mu <- ac.ranking(R, asset.rank)
Sample estimate for second moment
ac.moments$sigma <- cov(R)
#’ Generate random portfolios for use in the optimization.
rp <- random_portfolios(init.portf, 5000)
rp
#’ Run the optimization using first and second moments estimated from #’ Meucci’s Fully Flexible Views framework using the moments we calculated
opt.meucci <- optimize.portfolio(R,
init.portf,
optimize_method="random",
rp=rp,
trace=TRUE,
momentargs=m.moments)
opt.meucci
#’ Run the optimization using first moment estimated based on Almgren and Chriss, #’ “Portfolios from Sorts”. The second moment uses the sample estimate.
opt.ac <- optimize.portfolio(R,
init.portf,
optimize_method="random",
rp=rp,
trace=TRUE,
momentargs=ac.moments)
opt.ac
#’ For comparison, run the optimization using sample estimates for first and #’ second moments.
opt.sample <- optimize.portfolio(R,
init.portf,
optimize_method="random",
rp=rp,
trace=TRUE)
opt.sample
#’ Here we plot the optimal weights of each optimization.
chart.Weights(combine.optimizations(list(meucci=opt.meucci,
ac=opt.ac,
sample=opt.sample)),
ylim=c(0,1), plot.type="barplot")
#’ Here we define a custom moment function to estimate moments based on #’ relative ranking views. #’ Asset are ranked according to a momentum or reversal view based on the #’ previous n periods.
moment.ranking <- function(R, n=1, momentum=TRUE, method=c("meucci", "ac")){
# Moment function to estimate moments based on relative ranking of
# expected returns.
method <- match.arg(method)
# Use the most recent n periods of returns
tmpR <- apply(tail(R, n), 2, function(x) prod(1 + x) - 1)
if(momentum){
# Assume that the assets with the highest return will continue to outperform
asset.rank <- order(tmpR)
} else {
# Assume that the assets with the highest return will reverse
asset.rank <- rev(order(tmpR))
}
switch(method,
meucci = {
# Meucci Fully Flexible Views framework
# Prior probabilities
p <- rep(1 / nrow(R), nrow(R))
# Relative ordering view
moments <- meucci.ranking(R, p, asset.rank)
},
ac = {
# Almgren and Chriss Portfolios from Sorts
moments <- list()
moments$mu <- ac.ranking(R, asset.rank)
# Sample estimate for second moment
moments$sigma <- cov(R)
}
)
return(moments)
}
#’ Here we run out of sample backtests to test the out of sample performance using the different frameworks to express our views on relative asset return ranking.
opt.bt.meucci <- optimize.portfolio.rebalancing(R, init.portf,
optimize_method="random",
rebalance_on="quarters",
training_period=100,
rp=rp,
momentFUN="moment.ranking",
n=2,
momentum=TRUE,
method="meucci")
opt.bt.ac <- optimize.portfolio.rebalancing(R, init.portf,
optimize_method="random",
rebalance_on="quarters",
rp=rp,
momentFUN="moment.ranking",
n=2,
momentum=TRUE,
method="ac")
opt.bt.sample <- optimize.portfolio.rebalancing(R, init.portf,
optimize_method="random",
rebalance_on="quarters",
training_period=100,
rp=rp)
#’ Compute returns and chart performance summary.
ret.meucci <- Return.portfolio(R, extractWeights(opt.bt.meucci))
ret.ac <- Return.portfolio(R, extractWeights(opt.bt.ac))
ret.sample <- Return.portfolio(R, extractWeights(opt.bt.sample))
ret <- cbind(ret.meucci, ret.ac, ret.sample)
colnames(ret) <- c("meucci.rank", "ac.rank", "sample")
charts.PerformanceSummary(ret, main="Ranking Views Performance")